package com.chance.cc.crawler.prod.command.job.domain.news.zhihu;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.news.NewsCommonScript;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
public class ZHSearchTraceAWeekCrawlerScheduleJob extends NewsCommonScript {

    public static final String domain = "zhihu";

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
//        System.out.println(crawlerJob().generateCrawlerTriggerKey());
    }

    public static CrawlerJob publishCrawlerScheduleJobInfo(){


        CrawlerJob jdCrawlerJob = crawlerJob();

        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(jdCrawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("知乎搜索关键词一小时追溯定时采集");
        crawlerScheduleJob.setCrawlerKey(jdCrawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return jdCrawlerJob;

    }

    /**
     * 知乎账号：
     * 17765109381
     * chancedata123
     *
     * taobao采集
     * @return
     */
    public static CrawlerJob crawlerJob() {

        FilterInfo filterInfo = new FilterInfo();
        filterInfo.setFilter(CrawlerEnum.CrawlerRecordFilter.count);
        filterInfo.setCountTotalNum(100);

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl("https://www.zhihu.com/api/v4/search_v3")
                .httpHead("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36")
                .httpHead("cookie","_zap=36be83f0-66b6-41a4-a82e-557717e66c60; _xsrf=wYqhPySPtvwQz5FUxh4MMTnnUlsxOXwq; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1619693508; d_c0=\"AHAV78neHBKPTrVl39GEyRv-0UDTaj6e4Ag=|1603955096\"; KLBRSID=53650870f91603bc3193342a80cf198c|1619693540|1619693506; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1619693542; captcha_session_v2=\"2|1:0|10:1619693507|18:captcha_session_v2|88:dXhaeTNMRS9WNmxwWGFwc080NzJacm9BV0xHb0dDcDc2TUJlMDNSQzN4UWtNQ1QzKzlrQTRKK1FqZDBmNmZFMw==|0e2dbf994d6425f9edfe2d88404a3735b99a5e0299f88989c7…NwamFJSmU5STZ0WTA4QVNwNHY5d3gxNmZ1eW1CTVZKSy1tdGZyXy0waUZzSnk4MUdDQ1I3ZkU2QTlpczI3ejRUUl94bjdNdnJfSVI5X0dnbWJ5UmtOSXNTNU5FQVVZNVlSbjdnRE1PbnJuNlFHRzVSQzBHZ2RuR1NlTUhGclpDMGZVN2guMHBLRERBak9LOC1ncXZWbDhJbWdxZTlya2RfLXB0Mm1rdkpERXh1Vi1jYnJqMyJ9|2a3339468c07258c806496fe082b35c246281a9782cfe08aff70d877309f0edd\"; z_c0=\"2|1:0|10:1619693522|4:z_c0|92:Mi4xaEhwYklBQUFBQUFBY0JYdnlkNGNFaVlBQUFCZ0FsVk4wdDEzWVFCMnU2eUg1cUotb0FEVFVrOHZUSjBXNmZETW5n|288c140e5291360a2d65a26571aec120dd8fcedb2047be9c496e3cc8bd2900c1\"; tst=r")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .turnPageFilterInfo(filterInfo)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null))
                .proxy(proxy)
                .build();

        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addSite("search");
        requestRecord.tagsCreator().bizTags().addDomain(domain);

        CrawlerRecord articleFilterCrawlerRecord = new CrawlerRecord();
        articleFilterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        articleFilterCrawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*7,null));
        requestRecord.tagsCreator().bizTags().addCustomKV("article_record_filter_info", JSON.toJSONString(articleFilterCrawlerRecord));

        CrawlerRecord commentFilterCrawlerRecord = new CrawlerRecord();
        commentFilterCrawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.key);
        commentFilterCrawlerRecord.addFilterInfo(FilterUtils.redisFilterKeyInfo("filter-zhihu-search-comment-queue"));
        requestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info", JSON.toJSONString(commentFilterCrawlerRecord));

        requestRecord.tagsCreator().bizTags().addCustomKV("time_zone", "a_week");
        requestRecord.tagsCreator().bizTags().addCustomKV("scheduler_type", "a_week");

        //初始 comment crawler request record
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,"search","a_week","queue");
        CrawlerRequestRecord initNewsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("zhihu_news",turnPageItem)
                .httpUrl(metaServiceHttpPrefix+"/v1/meta/zhihu/keys?site=search")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .crawlerJobThreadNumber(15)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-","search","a","week",CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline(null,kafkaTopic,null)
                .requestRecord(requestRecord)
                .supportRecord(initNewsCrawlerRecord)
                .build();
    }

}
